package com.example.home.controller;

import com.example.home.Dao.MajorsRepository;
import com.example.home.Entities.Majors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("majors")
public class MajorsController {

    @Autowired
    MajorsRepository majorsRepository;

    @GetMapping
    public String outStudentForm1(Model model){
        List<Majors> majors1 = majorsRepository.findAll();
        model.addAttribute("major2",majors1);
        return "major/list-majorOne";
    }


    @GetMapping("/new")
    public String outStudentForm(Model model){

        Majors majors = new Majors();
        model.addAttribute("majorOr",majors);
        return "major/Majors";
    }

    @PostMapping("/save")
    public String CreateStudent(Model model,Majors majors){

        //处理保存数据库的行为
       majorsRepository.save(majors);
        //使用定位防止重复提交
        return "redirect:/majors/new";

    }

}
